package user;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import utilities.ServletUtilities;

import db.SessionFactoryUtil;
import db.Users;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		session.setAttribute("error", "Dostep zabrioniony");
		response.sendRedirect("/runWithJava/");
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String login = request.getParameter("login");
		String haslo = request.getParameter("haslo");
		Users user = UsersHome.getUserByLoginAndHaslo(ServletUtilities.filter(login), ServletUtilities.filter(haslo) );
		if(user!=null) {			
			session.setAttribute("user", user);			
		} else {
			session.setAttribute("error", "Podano bledny login lub haslo");
		}

		response.sendRedirect("/runWithJava/");
	}

}
